package com.briup.ch12;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/10/20
 * @Description:
 */
import java.io.*;
import java.net.*;



public class MultiThreadServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(8989);
        System.out.println("服务器启动，端口8989");

        while (true) {
            // 接收客户端并创建线程处理
            Socket client = server.accept();
            new Thread(() -> {
                try (
                        BufferedReader in = new BufferedReader(
                                new InputStreamReader(client.getInputStream()));
                        PrintWriter out = new PrintWriter(client.getOutputStream(), true)
                ) {
                    String msg;
                    while ((msg = in.readLine()) != null) {
                        System.out.println("收到：" + msg);
                        if (msg.equalsIgnoreCase("quit")) break;
                        out.println(new StringBuilder(msg).reverse()); // 反转返回
                    }
                    System.out.println("客户端断开");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}